package com.quasar.hpatient.view.prescription;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.widget.Toolbar;

import com.change360.calendarview.CalendarDay;
import com.change360.calendarview.MaterialCalendarView;
import com.change360.calendarview.OnDateChangedListener;
import com.change360.calendarview.OnMonthChangedListener;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.custommodel.Record;
import com.quasar.hpatient.model.custommodel.RecordState;
import com.quasar.hpatient.model.event.EventDecorator;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.DailyRecordData;
import com.quasar.hpatient.model.medicalmodel.MedicationRecordData;
import com.quasar.hpatient.model.medicalmodel.OnedayMedicationData;
import com.quasar.hpatient.onInterface.OnMedicationListener;
import com.quasar.hpatient.presenter.RemindMedicinePresenter;
import com.quasar.hpatient.utils.TimeZoneUtil;
import com.quasar.hpatient.view.personal.historyRecord.HistoryRecordDayActivity;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * 服药记录日历界面
 */
@EActivity(R.layout.activity_take_medicine)
public class TakeMedicineActivity extends BaseActivity implements OnMedicationListener, OnDateChangedListener, OnMonthChangedListener {

    RemindMedicinePresenter remindMedicinePresenter;
    CalendarDay calendarDay;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @ViewById(R.id.main_toolbar)
    Toolbar main_toolbar;
    @ViewById(R.id.calendarView)
    MaterialCalendarView calendarView;

    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
    }

    @Override
    protected void initData() {
        remindMedicinePresenter = new RemindMedicinePresenter(this);
        //初始化日期
        calendarView.setCurrentDate(Calendar.getInstance());
        calendarView.setCurrentDate(new Date());
        calendarView.setSelectedDate(new Date());
        calendarView.setOnDateChangedListener(this);
        calendarView.setOnMonthChangedListener(this);
        calendarView.setMaximumDate(new Date());
        calendarDay = new CalendarDay();
    }

    @Override
    protected void onResume() {
        super.onResume();
        remindMedicinePresenter.MedicationRecordData(TimeZoneUtil.fastMoth(calendarDay), format.format(new Date()));
    }

    @Override
    public void onSuccess(AnotherResult<OnedayMedicationData> data) {
    }

    @Override
    public void onSuccessMedicationRecordDate(AnotherResult<MedicationRecordData> data) {

        if (data.getList() != null && data.getList().size() > 0) {
            new ApiSimulator(data.getList()).executeOnExecutor(Executors.newSingleThreadExecutor(), calendarDay);
        }

    }

    @Override
    public void OnSeccessLast(AnotherResult<OnedayMedicationData> data) {

    }

    @Override
    public void OnSeccessNext(AnotherResult<OnedayMedicationData> data) {

    }

    @Override
    public void onRefreshUI() {

    }

    //点击日历跳转
    @Override
    public void onDateChanged(MaterialCalendarView widget, CalendarDay date) {
        calendarDay = date;
        MedicationRecordActivity_.intent(this).calendarDay(date).start();
    }

    //点击上下各月
    @Override
    public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
        remindMedicinePresenter.MedicationRecordData(TimeZoneUtil.fastMoth(date), TimeZoneUtil.lastMoth(date));
    }

    @Override
    public void onError(String msg) {
        if(msg!=null){
            msg(msg);
        }
    }
    @Override
    public void onAddMutiTakeMedicine(DataStateResult dataStateResult) {

    }

    @Override
    public void AddOneTakeMedicine(DataStateResult dataStateResult) {

    }


    private class ApiSimulator extends AsyncTask<CalendarDay, Void, List<RecordState>> {

        private List<MedicationRecordData> dailyRecordDataList;

        ArrayList<RecordState> csdates = new ArrayList<>();
//        ArrayList<RecordState> lfdates = new ArrayList<>();

        public ApiSimulator(List<MedicationRecordData> dailyRecordDataList) {
            this.dailyRecordDataList = dailyRecordDataList;
        }

        @Override
        protected List<RecordState> doInBackground(CalendarDay... voids) {

            ArrayList<RecordState> dates = new ArrayList<>();

            for (int i = 0; i < dailyRecordDataList.size(); i++) {
                RecordState recordState = new RecordState();
                recordState.drawable = R.mipmap.rc;

                Date de = null;
                try {
                    de = TimeZoneUtil.sdf.parse(dailyRecordDataList.get(i).getUseDateTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Calendar c = Calendar.getInstance();
                c.setTime(de);
                recordState.calendarDay = new CalendarDay(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
                dates.add(recordState);

                for (int j = 0; j < dailyRecordDataList.size(); j++) {
                    Calendar datec = Calendar.getInstance();
                    datec.setTime(new Date());
                    if (dailyRecordDataList.get(j).getStatus() == 0) {//0为异常
                        RecordState xxxx = new RecordState();
                        Date des = null;
                        ViseLog.d("异常的服药信息"+dailyRecordDataList.get(j).getUseDateTime());
                        try {
                            des = TimeZoneUtil.sdf.parse(dailyRecordDataList.get(j).getUseDateTime());
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Calendar b = Calendar.getInstance();
                        b.setTime(des);
                        xxxx.calendarDay = new CalendarDay(b.get(Calendar.YEAR), b.get(Calendar.MONTH), b.get(Calendar.DAY_OF_MONTH));
                        csdates.add(xxxx);
                    }
                }
            }
            return dates;
        }

        @Override
        protected void onPostExecute(List<RecordState> calendarDays) {
            super.onPostExecute(calendarDays);
            List<CalendarDay> days = new ArrayList<>();

            for (int i = 0; i < calendarDays.size(); i++) {
                RecordState state = calendarDays.get(i);
                days.add(state.calendarDay);
            }
            Drawable drawable = getResources().getDrawable(R.mipmap.medication1);
            calendarView.addDecorator(new EventDecorator(drawable, days));

            List<CalendarDay> csdays = new ArrayList<>();
            for (int i = 0; i < csdates.size(); i++) {
                RecordState state = csdates.get(i);
                csdays.add(state.calendarDay);
            }
            calendarView.addDecorator(new EventDecorator(getResources().getDrawable(R.mipmap.medication3), csdays));
        }
    }

}
